package com.example.liang.basamovie.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.example.liang.basamovie.R;
import com.example.liang.basamovie.adapter.PastSelectAdapter;
import com.example.liang.basamovie.adapter.PastSelectAdapter2;
import com.example.liang.basamovie.bean.Recommend4;
import com.example.liang.basamovie.json.RecommendJson3;
import com.example.liang.basamovie.utils.Path;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.util.ArrayList;
import java.util.List;

public class PastSelectActivity extends AppCompatActivity {
    ImageView back;
    RecyclerView recyclerViewup,recyclerViewdown;
    List<Recommend4> data;
    List<Recommend4> data2=new ArrayList<>();
    Boolean clickstate=true;
    PastSelectAdapter paastSelectAdapter;
    PastSelectAdapter2 paastSelectAdapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_past_select);
        recyclerViewdown= (RecyclerView) findViewById(R.id.pastSelect_down);
        recyclerViewup= (RecyclerView) findViewById(R.id.pastSelect_up);
        back= (ImageView) findViewById(R.id.toolbar_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RecyclerView.LayoutManager manager1 = new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL,
                false);
        RecyclerView.LayoutManager manager2 = new LinearLayoutManager(this,
                LinearLayoutManager.HORIZONTAL,
                false);
        recyclerViewup.setLayoutManager(manager1);
        recyclerViewdown.setLayoutManager(manager2);
        getData();
    }
    private void getData() {
        Intent intent = getIntent();
        if(intent.getStringExtra("title")!=null) {
            String url = intent.getStringExtra("url");
            String title = intent.getStringExtra("title");
            String context = intent.getStringExtra("context");
            String image_url = intent.getStringExtra("image_url");
            String day=intent.getStringExtra("day");
            String month=intent.getStringExtra("month");
            Recommend4 recommend4=new Recommend4();
            recommend4.setImage_url(image_url);
            recommend4.setArticleContentUrl(url);
            recommend4.setTitle(title);
            recommend4.setDescription(context);
            recommend4.setDay(day);
            recommend4.setMonth(month);
            data2.add(recommend4);
            paastSelectAdapter=new PastSelectAdapter(PastSelectActivity.this,data2);
            paastSelectAdapter2=new PastSelectAdapter2(PastSelectActivity.this,data2);
            recyclerViewup.setAdapter(paastSelectAdapter);
            recyclerViewdown.setAdapter(paastSelectAdapter2);
            clickstate=false;
        }
        if(clickstate) {
            OkHttpUtils.get()
                    .url(Path.PastSelect_PATH)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            Log.i("info", "-------往期精彩数据下载失败--------");
                        }
                        @Override
                        public void onResponse(String response) {
                            data = RecommendJson3.getRecommend4(response);
                            Log.i("info", "往期精彩数据下载成功，大小为:------" + data.size());
                            paastSelectAdapter = new PastSelectAdapter(PastSelectActivity.this, data);
                            paastSelectAdapter2 = new PastSelectAdapter2(PastSelectActivity.this, data);
                            recyclerViewup.setAdapter(paastSelectAdapter);
                            recyclerViewdown.setAdapter(paastSelectAdapter2);
                        }
                    });
               }

    }
}
